package com.hbu.product.hbumallproduct.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbu.common.utils.PageUtils;
import com.hbu.common.utils.R;
import com.hbu.product.hbumallproduct.entity.CategoryBrandRelationEntity;
import com.hbu.product.hbumallproduct.service.CategoryBrandRelationService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 品牌分类关联
 *
 * @author zhaoxiaohan
 * @email 13315217827@163.com
 * @date 2022-10-27 22:52:06
 */
@RestController
@RequestMapping("hbumallproduct/categorybrandrelation")
@AllArgsConstructor
public class CategoryBrandRelationController {

    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取分类关联的品牌
     */
    @RequestMapping("/brands/list")
    public R getBrandList(@RequestParam("catId") Long catId) {
        return R.ok().put("data", categoryBrandRelationService.getBrandList(catId));
    }

    /**
     * 根据品牌id获取该品牌关联的所有分类
     */
    @RequestMapping(value = "/catelog/list", method = RequestMethod.GET)
    public R catelogList(@RequestParam("brandId") Long brandId) {
        return R.ok().put("data", categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId)));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
